package com.espirit.eap.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.opensymphony.xwork2.ActionContext;

/**
 * Don't populate the blank parameter to avoid creating the blank model object
 * 
 * @deprecated 2012-10-26 use the JS to filter the blank parameter in the front page
 * @author feng Date: 2009-5-19
 *
 */
public class ParametersInterceptor extends com.opensymphony.xwork2.interceptor.ParametersInterceptor {

	@SuppressWarnings("unchecked")
	@Override
	protected Map<String, Object> retrieveParameters(ActionContext ac) {
        Map parameters = ac.getParameters();
        Map noEmptyParameters = new HashMap(parameters.size());
        
        for (Iterator iterator = parameters.entrySet().iterator(); iterator.hasNext();) {
            Map.Entry entry = (Map.Entry) iterator.next();
            String name = entry.getKey().toString();
            // 1. filter the ext variable
            if (name.charAt(0) == '_' || name.startsWith("ext-comp")) {
            	continue;
            }
            
            // 2. filter the blank value
            Object value = entry.getValue();
            if (value instanceof String[]) {
            	String[] theValue = (String[])value;
            	if (theValue.length == 1 && StringUtils.isEmpty(theValue[0])) {
            		continue;
            	}
            }
            
            noEmptyParameters.put(name, value);
        }
        
        return noEmptyParameters;
    }
}